/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.filter.logfilter;

/**
 * 
 * @author wan
 */
public final class URLAbstraction {
	private URLAbstraction() {
		
	}
/**
 * 
 * @param url parameter
 * @return
 */
	// 截取url及referer规则
	public static String getPartURL(String url) {
		String temp1 = "";
		String temp2 = "";

		int questionMarkIndex = url.indexOf("?");
		int headLength = 0;

		// 对于盘古url的处理
		if (url.startsWith("http://www.panguso.com/")) {
			if (questionMarkIndex < 0) {
				return url;
			} else {
				String temp0 = url.substring(0, questionMarkIndex);
				return temp0;
			}
		} else { // 对于普通url的处理，截取二级域名
			// 截取问号之前的内容
			if (questionMarkIndex < 0) {
				temp2 = url.substring(url.indexOf("//") + 2); // http后的内容
				headLength = url.indexOf("//") + 2;
				temp1 = url;
			} else {
				temp1 = url.substring(0, questionMarkIndex); // 问号之前的内容
				temp2 = temp1.substring(temp1.indexOf("//") + 2);
				headLength = temp1.indexOf("//") + 2;
			}

			// 截取第一个/之前的内容
			int firstSplit = temp2.indexOf("/");
			if (firstSplit < 0) {
				// System.out.println("final：" + temp1);
				return temp1;
			} else {
				String temp3 = temp2.substring(firstSplit + 1); // 第一个/之后的内容
				int secondSplit = temp3.indexOf("/");
				if (secondSplit < 0) {
					// System.out.println("final:" + temp1);
					return temp1;
				} else {
					int secondIndex = firstSplit + secondSplit + 1;
					String temp4 = temp1.substring(0, secondIndex + headLength);
					// System.out.println("final:" + temp4);
					return temp4;
				}
			}
		}
	}
}
